package com.ifdom.thread.threadlocal;

/**
 * 使用 synchronized 关键字
 * synchronized 加锁后，只有持有锁的线程才允许访问临界区
 * 而threadLocal 采用空间换时间，每个线程创建一个变量副本
 * @Author ifredomvip@gmail.com
 * @Date 2022/9/29 16:08
 * @Version 1.0.0
 * @Description
 **/

public class WithSynchronizedDemo {

    public String getContent() {
        return content;
    }

    private String content;

    private void setContent(String content) {
        this.content = content;
    }


    public static void main(String[] args) {

        WithSynchronizedDemo demo = new WithSynchronizedDemo();


        for (int i = 0; i < 10; i++) {

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {

                    synchronized (WithSynchronizedDemo.class){
                        demo.setContent(Thread.currentThread().getName()+"数据");
                        System.out.println("执行任务: " + Thread.currentThread().getName() + "====数据：" + demo.getContent());
                    }

                }
            });

            thread.setName("线程" + i);
            thread.start();
        }

    }
}
